<template>
  <el-dialog
    :title="!dataForm.choosePropertyId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" label-width="80px">
      <el-form-item label="商品筛选属性名" prop="choosePropertyName" label-width="150px">
        <el-input v-model="dataForm.choosePropertyName" placeholder="商品筛选属性名"></el-input>
    </el-form-item>
    <el-form-item label="商品筛选类别名" prop="chooseTypeId" label-width="150px">
      <template>
        <el-select v-model="dataForm.chooseTypeId" placeholder="请选择商品筛选类别">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </template>
     </el-form-item>
     <el-form-item label="状态" size="mini" prop="choosePropertyState" label-width="150px">
        <el-radio-group v-model="dataForm.choosePropertyState">
          <el-radio :label="0">启用</el-radio>
          <el-radio :label="1">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" :disabled="submitVisible" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        submitVisible: false,
        dataForm: {
          choosePropertyId: '',
          choosePropertyName: '',
          chooseTypeId: '',
          choosePropertyState: ''
        },
        dataRule: {
          choosePropertyName: [
            { required: true, message: '商品筛选属性名', trigger: 'blur' }
          ],
          chooseTypeId: [
            { required: true, message: '商品筛选类名', trigger: 'blur' }
          ],
          choosePropertyState: [
            { required: true, message: '请设置商品筛选属性状态', trigger: 'blur' }
          ]
        },
        options: []
      }
    },
    methods: {
      init (id) {
        this.$http({
          url: this.$http.adornUrl('/pig-center-biz/shopset/goodsChooseType/chooseTypeItemList'),
          method: 'get'
        }).then(({data}) => {
          this.options = data.items
        })
        this.dataForm.choosePropertyState = 0
        this.dataForm.choosePropertyId = id || ''
        this.visible = true
        this.submitVisible = false
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
        })
        if (this.dataForm.choosePropertyId) {
          this.$http({
            url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChooseProperty/info`),
            method: 'post',
            params: this.$http.adornParams({
              'choosePropertyId': this.dataForm.choosePropertyId
            })
          }).then(({data}) => {
            if (data && data.code === 0) {
              this.dataForm.choosePropertyName = data.goodsChooseProperty.choosePropertyName
              this.dataForm.chooseTypeId = data.goodsChooseProperty.chooseTypeId
              this.dataForm.choosePropertyState = data.goodsChooseProperty.choosePropertyState
            }
          })
        }
      },

      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.submitVisible = true
            this.$http({
              url: this.$http.adornUrl(`/pig-center-biz/shopset/goodsChooseProperty/${!this.dataForm.choosePropertyId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'choosePropertyId': this.dataForm.choosePropertyId || undefined,
                'choosePropertyName': this.dataForm.choosePropertyName,
                'chooseTypeId': this.dataForm.chooseTypeId,
                'choosePropertyState': this.dataForm.choosePropertyState
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
<style>

</style>
